/* 
		@parms url 接口地址
		@parms method 请求方式
		@parms data 参数
		@parms isUpload 默认false 是否上传
 */


const BASE_URL = "http://192.168.12.169:8080/api" //公共请求头
// const BASE_URL = "http://10.173.43.136:8080/api" //公共请求头
// const BASE_URL = "http://127.0.0.1:8080/api"
// const BASE_URL = "http://192.168.31.38:8080/api" //公共请求头
// const BASE_URL = "http://10.173.225.198:8080/api"


// isUpload  是否上传文件


const requestJson = (url, method, data, isUpload = false) => {
	const TOKEN = uni.getStorageSync('WeTeamToken') //TOKEN
	return new Promise((resolve, reject) => {
		if (!isUpload) {
			uni.request({
				url: BASE_URL + url, //仅为示例，并非真实接口地址。
				data: data,
				method: method,
				header: { //请求头可自定义
					'Content-Type': 'application/json',
					'X-Access-Token': TOKEN
				},
				success: (res) => { //具体捕获请看自己接口返回的形式
					if (res.data.code == 200 || res.data.code == 0 || res.data.code == 1204) {
						resolve(res)
					} else {
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: res.data.message,
							success(res) {
								if (res.confirm) {
									// console.log('用户点击确定')
									// uni.navigateBack({})
								} else if (res.cancel) {
									// console.log('用户点击取消')
								}
							}
						})
					}
				},
				fail(error) {
					reject(error)
				},
				complete() {

				}
			});
		} else {
			uni.uploadFile({
				url: _url,
				filePath: data,
				name: 'file',
				header: {
					'X-Access-Token': TOKEN
				},
				formData: {
					file: data,
					fileType: '1',
					pathType: "1"
				},
				success: (uploadFileRes) => {
					let parms = JSON.parse(uploadFileRes.data)
					let imgurls = parms.result.filePath
					resolve(imgurls)
				},
				fail: () => {
					uni.showToast({
						title: '图片上传失败',
						icon: 'none'
					})
				}
			});
		}

	})
}

const request = (url, method, data, isUpload = false) => {
	const TOKEN = uni.getStorageSync('WeTeamToken') //TOKEN
	return new Promise((resolve, reject) => {
		if (!isUpload) {
			uni.request({
				url: BASE_URL + url, //仅为示例，并非真实接口地址。
				data: data,
				method: method,
				header: { //请求头可自定义
					'Content-Type': 'application/x-www-form-urlencoded',
					'X-Access-Token': TOKEN
				},
				success: (res) => { //具体捕获请看自己接口返回的形式
					if (res.data.code == 200 || res.data.code == 0 || res.data.code == 1204) {
						resolve(res)
					} else {
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: res.data.message,
							success(res) {
								if (res.confirm) {
									// console.log('用户点击确定')
									uni.navigateBack({})
								} else if (res.cancel) {
									// console.log('用户点击取消')
								}
							}
						})
					}
				},
				fail(error) {
					reject(error)
				},
				complete() {

				}
			});
		} else {
			uni.uploadFile({
				url: BASE_URL + url,
				filePath: data,
				name: 'file',
				header: {
					'X-Access-Token': TOKEN
				},
				formData: {
					file: data,
					fileType: '1',
					pathType: "1"
				},
				success: (uploadFileRes) => {
					const response = JSON.parse(uploadFileRes.data);
					if (response.success) {
						resolve(response);
					} else {
						reject(new Error('File upload failed: ' + response.message));
					}
				},
				fail: (err) => {
					uni.showToast({
						title: '图片上传失败',
						icon: 'none'
					});
					reject(err);
				}
			});
		}

	})
}

const uploadAvatar = (url, data, ) => {
	const TOKEN = uni.getStorageSync('WeTeamToken') //TOKEN
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: BASE_URL + url,
			filePath: data,
			name: 'file',
			header: {
				'X-Access-Token': TOKEN
			},
			success: (uploadFileRes) => {
				let parms = JSON.parse(uploadFileRes.data)
				// console.log(parms)
				resolve(parms)
			},
			fail: () => {
				uni.showToast({
					title: '图片上传失败',
					icon: 'none'
				})
			}
		});
	})
}


export default {
	request, //向外暴露request
	requestJson,
	uploadAvatar
}